/****************************************************
	文件：LevelMap.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/10/13 17:2:39
	功能：
*****************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using HTUtility;
using HTFramework;
using HTFramework.ResKit;

namespace HTDemo01
{
    public class LevelMap : Singleton<LevelMap>
    {
        public int Width { get; set; }
        public int Height { get; set; }

        /// <summary>
        /// 地图数据
        /// </summary>
        public Item[,] MapData;
        private ClassObjectPool<Item> mItemPool;
        private System.Random mRandom;

        public void Init()
        {
            mItemPool = ClassPoolMgr.Instance.GetOrCreateObjectPool<Item>(180);
            mRandom = new System.Random(LevelMgr.Instance.RandomSeed);

            HTLogger.Info("LevelMap init done.");
        }

        /// <summary>
        /// 构建地图
        /// </summary>
        public void BuildMap()
        {
            HTLogger.Debug("开始读取地图数据");
            ReadMapData();
            HTLogger.Debug("开始构建地图");
            GameObject go;
            Item temp;
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    temp = MapData[i, j];
                    go = LevelMgr.Instance.GetItemPool(temp.ItemType).Spawn(GOLoader.Instance.SceneTrans, true);
                    //go = GOLoader.Instance.LoadSync(GetItemResPath(temp.ItemType));
                    go.transform.position = new Vector3(temp.PosX, temp.PosY, 0);
                    MapData[i, j].Go = go;
                }
            }
        }

        /// <summary>
        /// 读取地图数据
        /// </summary>
        private void ReadMapData()
        {
            Width = LevelMgr.Instance.LevelData.Width;
            Height = LevelMgr.Instance.LevelData.Height;
            MapData = new Item[Width, Height];
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    MapData[i, j] = Spawn(i, j, GetRandomItemType());
                }
            }
        }

        /// <summary>
        /// 生成 Item
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="itemType"></param>
        /// <returns></returns>
        public Item Spawn(int x, int y, ItemType itemType)
        {
            Item item = mItemPool.Spawn(true);
            item.X = x;
            item.Y = y;
            item.ItemType = itemType;
            return item;
        }
        /// <summary>
        /// 回收 Item
        /// </summary>
        /// <param name="item"></param>
        public void Recycle(Item item)
        {

        }

        /// <summary>
        /// 获取随机元素类型
        /// </summary>
        /// <returns></returns>
        private ItemType GetRandomItemType()
        {
            int num = mRandom.Next(1, 100);
            int index = 0;
            for (int k = 0; k < LevelMgr.Instance.ItemProbabilityLst.Count; k++)
            {
                if (num <= LevelMgr.Instance.ItemProbabilityLst[k])
                {
                    index = k;
                    break;
                }
            }
            return (ItemType)LevelMgr.Instance.ItemGroupLst[index];
        }
        /// <summary>
        /// 获取元素预制体路径
        /// </summary>
        /// <param name="itemType">元素类型</param>
        /// <returns></returns>
        private string GetItemResPath(ItemType itemType)
        {
            return string.Format("{0}{1}.prefab", GConsts.ITEM_PREFAB_PREFIX_PATH, (int)itemType);
        }
    }
}
